<template>
  <div class="login-container flex items-center">
    <div class="logo fixed left-5 top-5 flex logo-item">
      <div class="border-white border-r ">
        <img class="logo-img" src="@/images/login-font-logo.png" alt />
      </div>
      <div class>
        <img class="title-img" src="@/images/text-left-top.png" alt />
      </div>
    </div>
    <div class="main w-full flex items-center justify-center">
      <img class="login-left mr-28" src="@/images/login-left.png" alt />

      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form bg-white rounded-lg min-w-max"
        auto-complete="on"
        label-position="left"
      >
        <div class="title-container flex items-center justify-center h-20">
          <img src="@/images/text-hysy.png" alt />
        </div>

        <el-form-item prop="username">
          <el-input
            class
            ref="username"
            v-model="loginForm.username"
            placeholder="请输入账户名或手机号码"
            name="username"
            type="text"
            tabindex="1"
            auto-complete="on"
          >
            <img slot="prefix" class="w-4 mt-3 ml-1" src="@/images/user.png" alt />
          </el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="密码"
            name="password"
            tabindex="2"
            show-password
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          >
            <img slot="prefix" class="w-4 mt-3 ml-1" src="@/images/suo.png" alt />
          </el-input>
          <!-- <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>-->
        </el-form-item>

        <div class="flex justify-end">
          <span
            class="cursor-pointer mr-3 text-gray-600 hover:text-gray-800"
            @click="forgotPassword"
          >忘记密码?</span>
        </div>

        <el-button
          :loading="loading"
          type="primary" 
          class="login"
          @click.native.prevent="handleLogin"
        
        >登录</el-button>
        <div class="mb-5 text-center" v-if="false">
          <span>还没账号？</span>
          <a class="text-blue-500" :href="supplier_enter_link">请申请</a>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import { getToken, setToken, removeToken, cook } from '@/utils/auth'
import store from '@/store'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入账户名或者手机号码'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不允许少于6位'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: '',
      },
      loginRules: {
        //username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        //password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      supplier_enter_link: '',
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        const that = this
        const { username, password, redirect } = route.query || {}
        that.redirect = redirect
        if (username && password) {
          that.loginForm = { username, password }
          that.passwordType = ''
        }
      },
      immediate: true,
    },
  },
  mounted() {
    const { appsecret } = this.$route.query
    if (!!appsecret) {
      this.crmLogin(appsecret)
    }
    let host = window.location.host
    let protocol = window.location.protocol
    this.supplier_enter_link = protocol + '//' + host + '/#/supLogin'
  },
  methods: {
    // showPwd() {
    //   const that = this;
    //   that.passwordType = that.passwordType === 'password' ? '' : 'password';
    //   that.$nextTick(() => {
    //     that.$refs.password.focus();
    //   });
    // },
    async handleLogin() {
      const that = this
      console.log('this',that.loginForm)
      try {
        let login = await that.$.user.login(that.loginForm)
        let { username, button, menu_list, type } =
          login.result

        // store.dispatch('user/resetToken').then(() => {
        //   location.reload();
        // });
        await store.dispatch('user/setSupplierType', type) // 用户类型

        //菜单
        localStorage.setItem('menuList', JSON.stringify(menu_list))

        //权限按钮
        localStorage.setItem('autoButton', button)

        //账号类型
        localStorage.setItem('userType', type)
        localStorage.setItem('userNewType', type)
        //账号类型
        // localStorage.setItem('userNewType', type)
        cook('username', username)
    
        that.loading = false

        let u = type === 2 ? '/' : that.redirect || '/'
        that.$router.push({ path: u })
      } catch (e) {
        console.log(e)
      }
    },
    async crmLogin(appsecret) {
      const that = this
      try {
        that.$message({ type: 'success', message: '自动登录中，请稍等...' })
        let login = await that.$.user.crmLogin({
          appsecret: appsecret,
        })
        let { username, button, menu_list, type,  } =
          login.result

        await store.dispatch('user/setSupplierType', type) // 用户类型
  
        //菜单
        localStorage.setItem('menuList', JSON.stringify(menu_list))

        //权限按钮
        localStorage.setItem('autoButton', button)

        //账号类型
        localStorage.setItem('userType', type)
        

        cook('username', username)
        that.$router.push({ path: that.redirect || '/' })
        that.loading = false
      } catch (e) {
        console.log(e)
      }
    },
    forgotPassword() {
      this.$message({
        type: 'warning',
        message: '内部员工请联系运营组',
      })
    },
  },
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

/* reset element-ui css */
.login-container {
  .logo-item{
    align-items: center;
    .logo-img{
      padding-right: 12px;
    }
    .title-img{
      margin-left: 12px;

    }
  }
  .el-input {
    // display: inline-block;
    // height: 47px;
    // width: 85%;
    // border-radius: 200px !important;

    input {
      // -webkit-appearance: none;
      // padding: 12px 5px 12px 15px;
      // height: 47px;
      border-radius: 200px;
    }
  }

  // .el-form-item {
  //   border: 1px solid #eee;
  //   border-radius: 5px;
  // }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  background: url('../../images/login-bg.png') no-repeat center;
  background-size: 100% 100%;

  .main {
    .login-left {
      width: 834px;
    }
    .login-form {
      width: 460px;
      padding: 0 35px;
      overflow: hidden;
    }
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  // .svg-container {
  //   padding: 0 5px 6px 15px;
  //   color: #889aa4;
  //   vertical-align: middle;
  //   width: 34px;
  //   display: inline-block;
  // }

  .title-container {
    position: relative;

    .title {
      font-size: 36px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 18px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}

.login {
  width: 100%;
  margin-bottom: 30px;
  letter-spacing: 5px;
  font-size: 18px;
  height: 42px;
  line-height: 40px;
  padding: 0;
  margin-top: 20px;
}
</style>
